package cn.edu.cdcas.interceptor;

import cn.edu.cdcas.entity.Student;
import cn.edu.cdcas.utils.Constant;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @描述:登陆拦截器
 * @作者 :HuanGe
 * @创建日期 :2015年6月6日 下午4:32:21
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    private static final String[] IGNORE_URI = {"/login"};

    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {

    }

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        Student student = (Student) (request.getSession()).getAttribute(Constant.USER_IN_SESSION);
        String url = request.getRequestURI();
        for (String s : IGNORE_URI) {
            if (url.contains(s)) {
                return true;
            }
        }
        if (student == null) {
            response.sendRedirect("login");
            return false;
        }
        return true;
    }

}
